package com.usian.wemedia.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usian.apis.wemedia.WmUserControllerApi;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.media.pojos.WmUser;
import com.usian.wemedia.service.impl.WmUserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/user")
public class WmUserController implements WmUserControllerApi {
    @Autowired
    private WmUserServiceImpl wmUserService;

    /**
     * 保存自媒体用户
     * @param wmUser
     * @return
     */
    @Override
    @PostMapping("/save")
    public ResponseResult save(@RequestBody WmUser wmUser) {
        if (wmUser == null || wmUser.getName() == null || wmUser.getApUserId() == null)
            return wmUserService.errorResult;
        wmUserService.save(wmUser);
        return wmUserService.Sucess;
    }

    /**
     * 按照姓名查询自媒体用户
     * @param name
     * @return
     */
    @Override
    @GetMapping("/findByName/{name}")
    public WmUser findByName(@PathVariable String name) {

        if (name == null) return null;
        List<WmUser> list = wmUserService.list(Wrappers.<WmUser>lambdaQuery().eq(WmUser::getName, name));
        if (list != null && list.size() != 0) {
            return list.get(0);
        } else {
            return null;
        }

    }

    /**
     * 根据自媒体用户id查询到自媒体用户
     * @param id
     * @return
     */
    @GetMapping("/findOne/{id}")
    @Override
    public WmUser findWmUserById(@PathVariable Integer id) {
        return (WmUser) wmUserService.getById(id);
    }


}
